

#region Using directives

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using Microsoft.WindowsMobile.Status;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.Telephony;

#endregion
/** 
COPYRIGHT (C) 2008 Piyusha Desai. All Rights Reserved.
* Read Licence.txt before using this application.
*/
namespace SmsBuddy
{
 
    // If miss call detected when system started it transfers control here.
  
    public class MissCall : System.Windows.Forms.Form
    {
        private Label label1;
        private Timer timer;

        string callerInfo = "";
        int counter = 15;
        string message = "";

        SystemState missedCallNotification;

        Contact contact = null;
        Buddy buddy = new Buddy();
        Group group = null;
        private PictureBox imgContact;
        private MenuItem mnuCancelSend;
        private MenuItem menuOptions;
        private Label lblTimer;
        private MenuItem mnuCallBack;
        private MenuItem menuSendNow;
     
     

        private System.Windows.Forms.MainMenu mainMenu1;

        public MissCall()
        {
            InitializeComponent();
        }

       
  
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

      
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
   
        private void InitializeComponent()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.mnuCancelSend = new System.Windows.Forms.MenuItem();
            this.menuOptions = new System.Windows.Forms.MenuItem();
            this.mnuCallBack = new System.Windows.Forms.MenuItem();
            this.menuSendNow = new System.Windows.Forms.MenuItem();
            this.label1 = new System.Windows.Forms.Label();
            this.timer = new System.Windows.Forms.Timer();
            this.imgContact = new System.Windows.Forms.PictureBox();
            this.lblTimer = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.Add(this.mnuCancelSend);
            this.mainMenu1.MenuItems.Add(this.menuOptions);
            // 
            // mnuCancelSend
            // 
            this.mnuCancelSend.Text = "Cancel Send";
            this.mnuCancelSend.Click += new System.EventHandler(this.menuCancelSend_Click);
            // 
            // menuOptions
            // 
            this.menuOptions.MenuItems.Add(this.mnuCallBack);
            this.menuOptions.MenuItems.Add(this.menuSendNow);
            this.menuOptions.Text = "Options";
            // 
            // mnuCallBack
            // 
            this.mnuCallBack.Text = "Call Back";
            this.mnuCallBack.Click += new System.EventHandler(this.menuCallBack_Click);
            // 
            // menuSendNow
            // 
            this.menuSendNow.Text = "Send Now";
            this.menuSendNow.Click += new System.EventHandler(this.menuSendNow_Click);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(85, 45);
            this.label1.Text = " Missed Call Found";
            this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // timer
            // 
            this.timer.Interval = 1000;
            this.timer.Tick += new System.EventHandler(this.timer_Tick);
            // 
            // imgContact
            // 
            this.imgContact.Anchor = System.Windows.Forms.AnchorStyles.Right;
            this.imgContact.BackColor = System.Drawing.Color.Silver;
            this.imgContact.Location = new System.Drawing.Point(175, 0);
            this.imgContact.Name = "imgContact";
            this.imgContact.Size = new System.Drawing.Size(62, 63);
            // 
            // lblTimer
            // 
            this.lblTimer.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.lblTimer.Location = new System.Drawing.Point(0, 88);
            this.lblTimer.Name = "lblTimer";
            this.lblTimer.Size = new System.Drawing.Size(240, 178);
            // 
            // MissCall
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.ClientSize = new System.Drawing.Size(240, 266);
            this.Controls.Add(this.imgContact);
            this.Controls.Add(this.lblTimer);
            this.Controls.Add(this.label1);
            this.Menu = this.mainMenu1;
            this.Name = "MissCall";
            this.Text = "Missed Call";
            this.Load += new System.EventHandler(this.MissCall_Load);
            this.Closed += new System.EventHandler(this.MissedCallForm_Closed);
            this.ResumeLayout(false);

        }

        #endregion

        private void timer_Tick(object sender, EventArgs e)
        {
            if (counter == 0)
            {
                SendSms();
                Close();
            }
            else
            {
                DisplayTimer();
                counter--;
            }
        }

        private void DisplayTimer()
        {
            lblTimer.Text = "Caller :\n" + callerInfo ;
            lblTimer.Text += "The following SMS will be sent in " + counter.ToString() + " seconds: \n";
            if (message.Length > 0)
            {
                lblTimer.Text += message;
            }
            else
            {
                lblTimer.Text += "";
            }
            
        }

        private void MissCall_Load(object sender, EventArgs e)
        {
            missedCallNotification = new SystemState(Buddy.MissedCallIdName);
            //missedCallNotification.Changed += new ChangeEventHandler(missedCallNotification_Changed);

         
            imgContact.Left = (Width / 2) - (imgContact.Width / 2);

            #region State and Notification Broker: Get Last Caller Info

            // We use the SystemState class to get information about the
            // person who last called us and build a display string

            // Display the name of the caller
            callerInfo = SystemState.PhoneLastIncomingCallerName;
            callerInfo += "\n(";

            // Display the property of the phone number
            callerInfo += SystemState.PhoneLastIncomingCallerContactPropertyName;
            callerInfo += ") "; 

            // Display the phone number
            callerInfo += SystemState.PhoneLastIncomingCallerNumber;

            #endregion

            
            buddy = Buddy.Load();
            
            // If caller not from existing contact list then last call identify here.
            contact = SystemState.PhoneLastIncomingCallerContact;

            if (contact != null)
            {
                #region Custom Property
                // We do have a contact for the missed call.  Check the custom 
                // property for which group they belong to.  
                object id = contact.Properties[Buddy.ContactPropertyName];

                // if the custom property was never set, it will be NULL, so we will assume that
                // is group id '0'
                if (id == null)
                {
                    id = (int)0;
                }

                group = buddy.Groups[(int)id];
                #endregion 
            }
            else
            {
                contact = new Contact();
                contact.FileAs = "Unknown Caller";
                contact.MobileTelephoneNumber = SystemState.PhoneLastIncomingCallerNumber;
                group = buddy.UnknownGroup;
            }

            // Display the contact picture if any or put group icon
            if (contact.Picture != null)
            {
                imgContact.Image = contact.Picture;
            }
            else if (group.Image.Length > 0)
            {
                imgContact.Image = new Bitmap(group.Image);
            }

            message = GenerateMessage();

           
            timer.Enabled = true;
        }

      //  void missedCallNotification_Changed(object sender, ChangeEventArgs args)
      //  {
      //  }

        private void SendSms()
        {
            if (contact != null && group != null)
            {
                if (group.Enabled)
                {
                    // Send the message using system class with number and text.
                    try
                    {
                        SmsMessage sms = new SmsMessage(contact.MobileTelephoneNumber, message);
                        sms.Send();
                    }
                    catch (SmsException ex)
                    {
                        MessageBox.Show(ex.Message, "SMS Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "General Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    }
                }
            }
        }

        private string GenerateMessage()
        {
            string message = "I am busy ";

            if (group.Enabled)
            {
                #region State and Notification Broker: Appointments

                // If this group allows the sending of our current appointment, 
                // then we use the SystemState class to access the details 
                // about the current appointment.
                if (group.SendMeetingInfo &&
                    SystemState.CalendarAppointment != null)
                {
                    message += "I am busy in '";
                    // Get the current appointment subject
                    message += SystemState.CalendarAppointmentSubject;
                    message += "' and will be done at: ";

                    // Get the end time of the current appointment
                    message += SystemState.CalendarAppointmentEndTime.ToString();
                    message += ".  ";
                }

                #endregion

                if (group.SendMessage)
                {
                    message += group.Message;
                }
            }

            return message;
        }


        private void menuSendNow_Click(object sender, EventArgs e)
        {
            SendSms();
            Close();
        }

        private void menuCancelSend_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void MissedCallForm_Closed(object sender, EventArgs e)
        {
            missedCallNotification.Dispose();
        }
        
        //Phone class used to call again
        private void menuCallBack_Click(object sender, EventArgs e)
        {
            Phone phone = new Phone();
            phone.Talk(SystemState.PhoneLastIncomingCallerNumber);
            Close();
        }
    }
}
